﻿using ArticleandSiteData;
using ModelClassLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Email;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace SplitViewOptions.BusinessControl
{
    public sealed partial class UserListControl : UserControl
    {
        string api = " https://api.bmob.cn/1/users?order=-updatedAt";
        public UserListControl()
        {
            this.InitializeComponent();
        }

        private  async void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoginRootobject users = await BmobWebsiteData.GetLists<LoginRootobject>(api);
            List<LoginResult> listresults = new List<LoginResult>();
            foreach(var a in users.results)
            {
                if(a.isvisible=="1")
                {
                    listresults.Add(a);
                }
            }
            usersg.ItemsSource = listresults;
        }

        private async void EmailTapped(object sender, TappedRoutedEventArgs e)
        {
            TextBlock tn = sender as TextBlock;
            if (string.IsNullOrEmpty(tn?.Text))
            {
                return;
            }
            EmailMessage email = new EmailMessage();
            email.To.Add(new EmailRecipient(tn.Text));
            email.Subject = "hi";
            email.Body = "来自多彩浏览器的用户的问好";
            await EmailManager.ShowComposeNewEmailAsync(email);

        }

        private void PhoneTapped(object sender, TappedRoutedEventArgs e)
        {
            TextBlock tin = sender as TextBlock;
            if (string.IsNullOrEmpty(tin.Text)) return;
            Windows.ApplicationModel.Calls.PhoneCallManager.ShowPhoneCallUI(tin.Text, " 多彩");
        }

        private void MainWebspaceTapped(object sender, TappedRoutedEventArgs e)
        {
            try
            {
                TextBlock tin = sender as TextBlock;
                if(tin!=null)
                {
                    if (string.IsNullOrEmpty(tin.Text))
                    {
                        return;
                    }
                    if(tin.Text.StartsWith("www"))
                    {
                        tin.Text = "http://" + tin.Text;
                    }
                    if (!tin.Text.StartsWith("http"))
                    {
                        return;
                    }
                    App.Currenturl = new Uri(tin.Text, UriKind.RelativeOrAbsolute);
                }
                App.Mainframe.Navigate(typeof(HomePage));
            }
            catch (Exception ex)
            {
            }
        }
    }
    public class LoginRootobject
    {
        public LoginResult[] results { get; set; }
    }

    public class LoginResult
    {
        public string createdAt { get; set; }
        public string email { get; set; }
        public string isvisible { get; set; }
        public string mainwebspace { get; set; }
        public string objectId { get; set; }
        public string phone { get; set; }
        public string updatedAt { get; set; }
        public string username { get; set; }
        public string likesbox { get; set; }
        public string introducebox { get; set; }
    }
}

